package com.weaver.smartext.core.parser;


import com.weaver.classifier.summariser.SimpleSummariser;
import com.weaver.smartext.core.anno.Parse;
import com.weaver.smartext.core.domain.StructableTextDto;
import com.weaver.smartext.core.domain.TextAnalysis;
import com.weaver.smartext.core.enums.FileEnums;

import java.io.*;

/**
 * @Description: 普通句子或者一段话
 * @title: TextParser
 * @Author Star_Chen
 * @Date: 2023/4/4 20:30
 * @Version 1.0
 */
@Parse(fileType = FileEnums.TXT)
public class TextParser implements Parser<StructableTextDto> {

    @Override
    public StructableTextDto parser(String path) {
        return null;
    }

    @Override
    public StructableTextDto parser(File file) {
        return null;
    }

    /**
     * @Author Star_Chen
     * @Description 目前支持英文全翻译,中文的解析器用IK
     * @Date 10:52 2023/4/7
     * @Param [textAnalysis]
     * @return com.weaver.smarttext.core.domain.StructableTextDto
     **/
    @Override
    public StructableTextDto parser(TextAnalysis textAnalysis) {

        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(textAnalysis.getInputStream(), "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        StringBuffer sb = new StringBuffer();
        String data = null;
        while (true) {
            try {
                if (!((data = bufferedReader.readLine()) != null)) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            sb.append(data);
        }
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        StructableTextDto structableTextDto = new StructableTextDto();
        SimpleSummariser summariser = new SimpleSummariser();
        System.out.println(sb);
        String summarise = summariser.summarise(sb.toString(), 2);
        structableTextDto.setContent(summarise);

        return structableTextDto;


    }
}
